<!DOCTYPE html>
            
<HTML>
<HEAD>
<meta name="booktitle" content="Developing Applications With Objective Caml" >
 <meta charset="ISO-8859-1"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<META name="GENERATOR" content="hevea 1.05-7 of 2000-02-24">
<META NAME="Author" CONTENT="Christian.Queinnec@lip6.fr">
<LINK rel=stylesheet type="text/css" href="videoc-ocda.css">
<script language="JavaScript" src="videoc.js"><!--
//--></script>
<TITLE>
 Object-oriented languages: comparison with Java
</TITLE>
</HEAD>
<BODY class="regularBody">
<A HREF="book-ora202.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora204.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2> Object-oriented languages: comparison with Java</H2>Although Objective CAML sprang from the functional world, it is necessary to
compare its object-oriented extension to an important representative of the
object-oriented languages. We pick the Java language which, while
similar from the point of view of its implementation, differs strongly in
its object model and its type system.<BR>
<BR>
The Java language is an object-oriented language developed by the SUN
corporation. The main site for access to the language is the following:


<H3> Link </H3> <HR>

<A HREF="http://java.sun.com">http://java.sun.com</A>


<HR>

<BR>
<BR>
<A NAME="toc316"></A>
<H3> Main characteristics</H3>
The Java language is a language with classes. Inheritance is simple and
allows redefinition or overloading of inherited methods. Typing is static.
An inherited class is in a subtyping relationship with its ancestor class.<BR>
<BR>
Java does not have parameterized classes. One gets two types of
polymorphism: ad hoc by overloading, and of inclusion by
redefinition.<BR>
<BR>
It is multi-threading and supports the development of distributed
application whether using sockets or by invoking methods of remote objects
(Remote Method Invocation).<BR>
<BR>
The principles of its implementation are close to those of Objective CAML. A
Java program is compiled to a virtual machine (JVM). The loading of code
is dynamic. The code produced is independent of machine architectures,
being interpreted by a virtual machine. The basic datatypes are specified
in such a way as to guarantee the same representation on all architectures.
The runtime is equipped with a GC.<BR>
<BR>
Java has important class libraries (around 600 with the JDK,
which are supplemented by many independent developments). 
The main libraries concern graphical interfaces and I/O operations
integrating communication between machines.<BR>
<BR>
<A NAME="toc317"></A>
<H3> Differences with Objective CAML</H3>The main differences between Java and Objective CAML come from their type
system, from redefinition and from overloading of methods.
Redefinition of an inherited method must use parameters of exactly the same
type. Method overloading supports switching the method to use according to
the types of the method call parameters. In the following example class
<TT>B</TT> inherits from class <TT>A</TT>. Class <TT>B</TT> 
redefines the first version of the <TT>to_string</TT> method, 
but overloads the second
version. Moreover the <TT>eq</TT> method is overloaded since the type of the
parameter (here <TT>B</TT>) is not equal to the type of the parameter of the
inherited method (here <TT>A</TT>). In the end class <TT>B</TT> has two 
<TT>eq</TT> methods and two <TT>to_string</TT> methods.
<PRE>
class A {
  boolean eq (A o) {  return true;}
  String to_string (int n ) { }
}

class B extends A {
  boolean eq (B o) {  return true;}
  String to_string (int n ) { }
  String to_string (float x, float y)
}
</PRE>Although binding is late, overload resolution, that is determination of
the type of the method to use, is carried out on compilation.<BR>
<BR>
The second important difference derives from the possibility of casting the
type of an object, as would be done in C. In the following example, two
objects <TT>a</TT> and <TT>b</TT> are defined, of class <TT>A</TT> and <TT>B</TT>
respectively. Then three variables <TT>c</TT>, <TT>d</TT> and <TT>e</TT> are
declared while imposing a type constraint on the affected values.
<PRE>
{
  A a = new A ();
  B b = new B ();
  A c = (A) b;
  B d = (B) c;
  B e = (B) a;
}
</PRE>Since the type of <TT>b</TT> is a subtype of the type of <TT>a</TT>, the cast from 
<TT>b</TT> to <TT>c</TT> is accepted. In this case the type constraint may be
omitted. On the other hand the two following constraints require a
dynamic type test to be carried out to guarantee that the values in <TT>c</TT> 
and in <TT>a</TT> do in fact correspond to objects of class <TT>B</TT>. In this
program this is true for <TT>c</TT>, but false for <TT>a</TT>. So this last
case raises an exception. While this is useful, in particular for
graphical interfaces, these type constraints can lead to exceptions being
raised during exception due to erroneous use of types. In this Java is
a language typed partly statically and partly dynamically. Moreover the
absence of parameterized classes quite often obliges one to use this feature
to write generic classes.<BR>
<BR>
<HR>
<A HREF="book-ora202.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora204.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
